package controllers;

import play.*;
import play.libs.MimeTypes;
import play.mvc.*;
import play.mvc.Http.StatusCode;
import play.mvc.Scope.Session;

import siena.core.Base64;
import utils.constants.SessionConstants;
import utils.files.GAEFileService;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import models.*;
import models.Photo.Image;

public class Application extends BaseController {
	// TODO REMOVE THIS!!!!!!!!!! Static var for testing notifications
	static Integer total = 1;
	
    public static void index() {
    	boolean index = true;
        render(index);
    }
    
    public static void about() {
    	boolean about = true;
    	render(about);
    }
    
    public static void contact() {
    	boolean contact = true;
    	render(contact);
    }
    
    public static void login(String residence) throws Throwable {
    	if(residence == null || ("").equals(residence)) notFound();
		Subscriber subscriber = Subscriber.findByName(residence);
		if(subscriber == null) notFound();
		
    	flash.put("residence", subscriber.name);
    	
    	if(!isBrowserCompatible()) {
    		render("errors/browser.html");
    	} else {
    		render();
    	}
    	
    }
    
    //////////////////////////////////////////////////////
    // TEST METHODS   ////////////////////////////////////
    //////////////////////////////////////////////////////
    
    // This method is for test an admin page without validation.
    public static void test_admin() {
    	render("Application/admin.html");
    }
    
    public static void upload(File file) {
    	response.setHeader("location", "http://probando.voy/"+file.getName());
    	ok();
    }
    
    
    
    public static void notifications() {
    	ArrayList<Notification> notifications = new ArrayList<Notification>();
    	Data<Notification> data = new Data<Notification>();
    	
    	data.data = notifications;
    	for(int i = 0; i < total; i++) {
    		Notification notification = new Notification();
    		notification.message = "Notification " + i;
    		notifications.add(notification);
    	}
    	data.total = total;
    	data.data = notifications;
    	total++;
    	Type dataType = new TypeToken<Data<Subscriber>>(){}.getType();
		Gson gson = new Gson();
		renderJSON(gson.toJson(data, dataType));
    }
    
    public static boolean isBrowserCompatible() { 
    	final String MOZILLA_AGENT = "([A-Za-z]+/\\d\\.\\d)\\s(.*)\\s(([A-Za-z]+)/(\\d+))\\s([A-Za-z]+/((\\d{2}).*))"; // Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
    	final String CHROME_AGENT = "([A-Za-z]+/\\d\\.\\d)\\s(.*)\\s(.*)\\s(\\(.*\\))\\s([A-Za-z]+/((\\d{1,3}).*))\\s(.*)"; // Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
    	//final String IE_AGENT =""; //Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ESES)
    	
    	boolean compatible = false;
    	String browser = request.headers.get("user-agent").value();
        
    	if(browser.contains("Firefox")) {
    		Pattern p = Pattern.compile(MOZILLA_AGENT);
            Matcher m = p.matcher(browser);
            
            if(m.matches()) {
            	String version = m.group(m.groupCount());
            	try {
            		Integer v = Integer.parseInt(version);
            		if(v >= 10) {
            			compatible = true;
            		}
            	} catch(NumberFormatException e) {
            		Logger.error(e, "Error parsing user-agent!");
            	}
            	
            }
    	} else if(browser.contains("Chrome")) {
    		Pattern p = Pattern.compile(CHROME_AGENT);
            Matcher m = p.matcher(browser);
            
            if(m.matches()) {
            	String version = m.group(m.groupCount()-1);
            	try {
            		Integer v = Integer.parseInt(version);
            		if(v >= 10) {
            			compatible = true;
            		}
            	} catch(NumberFormatException e) {
            		Logger.error(e, "Error parsing user-agent!");
            	}
            }
    	} 
    	return compatible;
    }
   
}